#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <strings.h>
#include "../db/sql.h"
#include<cstring>
#include<strings.h>
#include<iostream>
#include<time.h>

using namespace std;


#define SQL_MAX 500

int Register(char *name,char *pwd){
	MYSQL mysql;
	
	int w = mysql_connect(mysql);
	if(w){
		return 1;
	}
	char sqlstr[SQL_MAX];
	snprintf(sqlstr,SQL_MAX,"INSERT INTO client_user(name,password,IPaddr) VALUES('%s','%s',-1)",name,pwd);
	//printf("sqlstr = %s\n",sqlstr);
	
	int ret = mysql_real_query(&mysql,sqlstr,(unsigned long)strlen(sqlstr));
	if(ret){
		return 2;
	}

	mysql_close(&mysql);
	
	return 0;
}

int login(int id,int fd){
	MYSQL mysql;
	int w = mysql_connect(mysql);
	if(w){
		return 1;
	}
	char sqlstr[SQL_MAX];
	snprintf(sqlstr,SQL_MAX,"UPDATE client_user set IPaddr=%d where id= %d",fd,id);
	int ret = mysql_real_query(&mysql,sqlstr,(unsigned long)strlen(sqlstr));
	if(ret){
		return 2;
	}
	mysql_close(&mysql);
	return 0;
}

int logout(int id){
	MYSQL mysql;
	int w=mysql_connect(mysql);
	if(w==1){
		return 1;
	}
	char sqlstr[SQL_MAX];
	snprintf(sqlstr,SQL_MAX,"UPDATE client_user set IPaddr=-1 where id= %d",id);
	int ret = mysql_real_query(&mysql,sqlstr,(unsigned long)strlen(sqlstr));
	if(ret){
		return 2;
	}
	mysql_close(&mysql);
	return 0;
}

int iptoid(int fd){
	MYSQL mysql;
	int w=mysql_connect(mysql);
	if(w){
		return 1;
	}
	char sqlstr[SQL_MAX];
	snprintf(sqlstr,SQL_MAX,"SELECT ID from client_user where IPaddr = %d",fd);
	
	int ret = mysql_real_query(&mysql,sqlstr,(unsigned long)strlen(sqlstr));
	if(ret){
		return 2;
	}

	MYSQL_RES *res;
	MYSQL_ROW row;

	res = mysql_store_result(&mysql);
	int cnt=0;
	int id;
	while(row = mysql_fetch_row(res)){
		id = atoi(row[0]);
		++cnt;
	}
	if(cnt==0)return 0;
	if(cnt>1)return -1;
	return id;
	
}


int nametoid(char *name){
	MYSQL mysql;
	int w=mysql_connect(mysql);
	if(w)return 1;

	char sqlstr[SQL_MAX];
	snprintf(sqlstr,SQL_MAX,"SELECT ID FROM client_user where name = '%s'",name);
	int ret = mysql_real_query(&mysql,sqlstr,(unsigned long)strlen(sqlstr));
	if(ret)return 2;

	MYSQL_RES *res;
	MYSQL_ROW row;

	res = mysql_store_result(&mysql);
	int cnt=0;
	int id;
	while(row = mysql_fetch_row(res)){
		id = atoi(row[0]);
		++cnt;
	}
	if(cnt==1)return id;
	return 0;
}

string idtoname(int id){

	MYSQL mysql;
	int w=mysql_connect(mysql);
	//if(w)return 1;
	 
	char sqlstr[SQL_MAX];
	snprintf(sqlstr,SQL_MAX,"SELECT name FROM client_user where id = %d",id);
	int ret = mysql_real_query(&mysql,sqlstr,(unsigned long)strlen(sqlstr));
	//if(ret)return 2;
	 
	MYSQL_RES *res;
	MYSQL_ROW row;
	 
	res = mysql_store_result(&mysql);
	int cnt=0;
	string name;
    while(row = mysql_fetch_row(res)){
	    name = row[0];
	    ++cnt;
	}
	if(cnt==1)return name;
	return 0;
}


string idtopwd(int id){
	MYSQL mysql;
	int w=mysql_connect(mysql);
	//if(w)return 1;
	char sqlstr[SQL_MAX];
	snprintf(sqlstr,SQL_MAX,"SELECT password FROM client_user where ID = %d",id);
	int ret = mysql_real_query(&mysql,sqlstr,(unsigned long)strlen(sqlstr));
	//if(ret)return 2;

	MYSQL_RES *res;
	MYSQL_ROW row;
	res = mysql_store_result(&mysql);
	int cnt=0;
	string pwd;
	while(row = mysql_fetch_row(res)){
		pwd = row[0];
		++cnt;
	}
	if(cnt==1)return pwd;
	return 0;
}


int finduser(char *name){
	MYSQL mysql;
	int w=mysql_connect(mysql);
	if(w)return 1;

	char sqlstr[SQL_MAX];
	snprintf(sqlstr,SQL_MAX,"SELECT ID FROM client_user where name = '%s'",name);
	int ret = mysql_real_query(&mysql,sqlstr,(unsigned long)strlen(sqlstr));
	if(ret)return 2;
				
	MYSQL_RES *res;
	MYSQL_ROW row;
	res = mysql_store_result(&mysql);
	int cnt=0;
	string pwd;
	int id=0;

	while(row = mysql_fetch_row(res)){
		id = atoi(row[0]);
		++cnt;
	}

	return id;
}
	
int checkfriend(int id,int id2){
	MYSQL mysql;
	int w=mysql_connect(mysql);
	if(w)return 1;

	char sqlstr[SQL_MAX];
	snprintf(sqlstr,SQL_MAX,"SELECT id FROM friend where id2 = %d AND id = %d",id,id2);
	int ret = mysql_real_query(&mysql,sqlstr,(unsigned long)strlen(sqlstr));
	if(ret)return 2;
	
	MYSQL_RES *res;
	MYSQL_ROW row;
	res = mysql_store_result(&mysql);
	int cnt=0;
	
	while(row = mysql_fetch_row(res)){
		++cnt;
	}

	if(cnt)return 3;
	return 0;
}

int addfriend(int id,int id2){
	if(checkfriend(id,id2))return 3;

	MYSQL mysql;
	int w=mysql_connect(mysql);
	if(w)return 1;
	
	char sqlstr[SQL_MAX];
	snprintf(sqlstr,SQL_MAX,"INSERT INTO friend(id,id2) values(%d,%d)",id,id2);
	int ret = mysql_real_query(&mysql,sqlstr,(unsigned long)strlen(sqlstr));
	if(ret)return 2;

	snprintf(sqlstr,SQL_MAX,"INSERT INTO friend(id,id2) values(%d,%d)",id2,id);
	ret = mysql_real_query(&mysql,sqlstr,(unsigned long)strlen(sqlstr));
	if(ret)return 2;
	return 0;
}

int delfriend(int id,int id2){
	if(checkfriend(id,id2)!=3)return 3;

	MYSQL mysql;
	int w=mysql_connect(mysql);
	if(w)return 1;
	
	char sqlstr[SQL_MAX];
	snprintf(sqlstr,SQL_MAX,"DELETE FROM friend WHERE id=%d AND id2=%d",id,id2);
	int ret = mysql_real_query(&mysql,sqlstr,(unsigned long)strlen(sqlstr));
	if(ret)return 2;
	
	snprintf(sqlstr,SQL_MAX,"DELETE FROM friend WHERE id=%d AND id2=%d",id2,id);
	ret = mysql_real_query(&mysql,sqlstr,(unsigned long)strlen(sqlstr));
	if(ret)return 2;
	return 0;
}

int sendmes(int id,int id2,char *mes){
	MYSQL mysql;
	    
	int w = mysql_connect(mysql);	
	if(w) return 1;
	
	time_t nowtime;
	time(&nowtime);
	struct tm *tim;
	tim = gmtime(&nowtime);

	char sqlstr[SQL_MAX];
	snprintf(sqlstr,SQL_MAX,"INSERT INTO message(id,id2,mes,status,time) VALUES(%d,%d,'%s',1,'%d-%d-%d-%d:%d:%d')"
	,id,id2,mes,1900+tim->tm_year,1+tim->tm_mon,tim->tm_mday,8+tim->tm_hour,tim->tm_min,tim->tm_sec);
	//printf("sqlstr = %s\n",sqlstr);
	 
	int ret = mysql_real_query(&mysql,sqlstr,(unsigned long)strlen(sqlstr));
	if(ret)return 2;

	mysql_close(&mysql);
}

int show(int id,int fd){
	MYSQL mysql;
	int w=mysql_connect(mysql);
	if(w)return 1;

	char sqlstr[SQL_MAX];
	snprintf(sqlstr,SQL_MAX,"select xxx.cnt,ppp.nam from (select id,COUNT(*) as cnt from message where id2 = %d AND status =1 group by id) as xxx,(select client_user.name as nam ,ttt.id2 as sec  from(select id2 from friend where id = %d) as ttt,client_user where ttt.id2 = client_user.id) as ppp where xxx.id = ppp.sec",id,id);

	int ret = mysql_real_query(&mysql,sqlstr,(unsigned long)strlen(sqlstr));
	if(ret)return 2;
	
	MYSQL_RES *res;
	MYSQL_ROW row;
	res = mysql_store_result(&mysql);

	write(fd,"now user:",sizeof("now user:"));
	string nna = idtoname(id);
	write(fd,nna.c_str(),sizeof(nna.c_str()));
	write(fd,"\n",sizeof("\n"));
	int cnt=0;
	
	while(row = mysql_fetch_row(res)){
		write(fd,"unread message:",sizeof("unread message:"));
		
		
		char pr[55];
		memset(pr,0,sizeof(pr));
		sprintf(pr,"%s",row[0]);

		write(fd,pr,sizeof(pr));

		write(fd,"     ",sizeof("     "));

		write(fd,row[1],sizeof(row[1]));
		write(fd,"\n",sizeof("\n"));
	}
	
	return cnt;
}


int read(int id,int id2,int fd){
	MYSQL mysql;
	int w=mysql_connect(mysql);
	if(w)return 1;
	char sqlstr[SQL_MAX];
	snprintf(sqlstr,SQL_MAX,"SELECT time,mes  FROM message where status = 1 AND id = %d AND id2 = %d",id2,id);
	
	int ret = mysql_real_query(&mysql,sqlstr,(unsigned long)strlen(sqlstr));
	if(ret)return 2;

	MYSQL_RES *res;
	MYSQL_ROW row;
	res = mysql_store_result(&mysql);
	
	while(row = mysql_fetch_row(res)){
				
		write(fd,row[0],sizeof(char)*strlen(row[0]));
		write(fd,"     ",sizeof("     "));
		
		write(fd,row[1],sizeof(row[1])*strlen(row[1]));
		write(fd,"\n",sizeof("\n"));
	}

	snprintf(sqlstr,SQL_MAX,"UPDATE message SET status = 0 where id = %d AND id2 = %d",id2,id);
	ret = mysql_real_query(&mysql,sqlstr,(unsigned long)strlen(sqlstr));
	if(ret)return 2;
	return 0;
}




